package com.example;

import org.junit.Test;

import java.util.*;

public class MapTest {

    @Test
    public void test01() {
        /* map相当于一个可变对象 */
        Map map = new HashMap();
        map.put("name", "张三");
        map.put("age", 20);
        map.put("sex", "男");
        List<String> list = new ArrayList<>();
        list.add("游戏");
        list.add("编程");
        map.put("hobby", list);
        System.out.println(map);
        map.put("color", "red");
        map.put("background-color", "#F00");
        map.put("name", "李四"); // 添加一个已有的key，会覆盖之前的数据
        System.out.println(map);
        Set set = map.keySet(); // key的set集合，hashSet
        System.out.println(set);
    }

    @Test
    public void test02() {
        Map<String, Integer> map = new HashMap<>();
        Map<String, Integer> map1 = new TreeMap<>();
        /* TreeMap，第一个泛型User必须具有排序规则 */
        Comparator<User> comparator = new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return 0;
            }
        };
        Map<User, Integer> map2 = new TreeMap<>(comparator);
    }

    @Test
    public void test03() {
        Map<String, Object> data = new HashMap<>();
        data.put("name", "张三"); // 添加
        data.put("age", 20);
        data.put("hobby", new ArrayList<>());
        System.out.println(data.get("name")); // 取出
        List<String> hobby = (List<String>)data.get("hobby");
        hobby.add("编程");
        hobby.add("游戏");
        System.out.println(data);
        data.remove("age"); // 通过key删除
        System.out.println(data);
        // 判断是否包含某个key
        System.out.println(data.containsKey("name"));
        System.out.println(data.size()); // key的数量
        // map的遍历
        Set<String> keys = data.keySet();
        for (String key : keys) {
            System.out.println("key:" + key + "; value:" + data.get(key));
        }
        // 把一个key,value组合视为一个元素，而map就是kv组合的单列集合
        // Map.Entry<String, Object>是单个键值对
        Set<Map.Entry<String, Object>> entries = data.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            System.out.println("key:" + entry.getKey()
                    + "; value:" + entry.getValue());
        }
    }

    @Test
    public void test04() {
        Properties properties = new Properties();
        // 默认存放String和String类型的键值对
        properties.setProperty("url", "http://localhost:3306/mydata");
        properties.setProperty("username", "root");
        properties.setProperty("password", "123456");
        properties.put(1, 2); // 不建议用
    }

    @Test
    public void test05() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        Collections.addAll(list, "3", "4", "5", "a", "b");
        System.out.println(list);
        fun(new String[] {"a", "b", "c", "d", "e", "f"});
        fun(1,2,true,false,"abc");
        /* 数组转集合 */
        List<String> list1 = Arrays.asList("1", "2", "3");
        System.out.println(list1);
        /* 集合转数组 */
        /*String[] array = (String[])list1.toArray();
        System.out.println(array);*/
        String[] array = new String[list1.size()];
        list1.toArray(array);
        System.out.println(Arrays.toString(array));
    }

    /* 可变参数可以当作数组一样用 */
    /* 可变参数必须放在形参列表的最后一个 */
    private static void fun(Object ... objects) {
        System.out.println(Arrays.toString(objects));
    }
}
